package com.yunji.base.business;

import com.yunji.base.domain.PayoutPersonDO;
import com.yunji.base.service.PayoutPersonService;
import com.yunji.common.base.BaseBusiness;
import com.yunji.common.enums.common.UserType;
import com.yunji.common.model.dto.LoginInfo;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.order.emuns.PayMode;
import com.yunji.system.context.SystemContext;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.user.business
 * @Title: PayoutPersonBusiness
 * @Description: 转账人员信息业务处理
 * History:
 * Date						Version			Author			Summary
 * ========================================================================
 * 2024-04-28 10:43:15		V1.0			HuaAo		新建类
 */
@Component
public class PayoutPersonBusiness extends BaseBusiness {
    @Resource
    PayoutPersonService personService;

    /**
     * @param person 用户转账信息
     * @Title updatePerson
     * @Desc 更新用户转账信息
     * @Date 2024-04-28 12:19:34.235
     */
    public void updatePerson(PayoutPersonDO person) {
        LoginInfo info = SystemContext.getUser();
        Assert.isTrue(BeanUtils.isNotNull(info), "未找到登录信息");

        Long userId = info.getUserId();
        Long belongId = info.getBelongId();
        String userType = info.getUserType();
        String userText = UserType.getText(userType);
        logs.info("【转账联系人】当前用户ID:{},所属ID:{},用户类型={}:{}", userId, belongId, userType, userText);

        Integer payMode = person.getPayMode();
        String payContent = PayMode.getText(payMode);
        logs.info("【转账联系人】提现方式={}:{}", payMode, payContent);
        Assert.isTrue((BeanUtils.equals(payMode, PayMode.WECHAT.getCode(), PayMode.ALIPAY.getCode())), "系统不支持该提现方式");

        PayoutPersonDO origin = personService.getByPayMode(belongId, payMode);

        PayoutPersonDO update = new PayoutPersonDO();
        BeanUtils.clone(person, update);

        if (BeanUtils.isNull(origin)) {
            update.setUserId(userId);
            update.setBelongId(belongId);
            update.setUserType(userType);
            update.setUserName(info.getNickname());
            Assert.isTrue(personService.createPayoutPerson(update), "联系人记录失败");
        } else {
            update.setPersonId(origin.getPersonId());
            Assert.isTrue(personService.updatePayoutPerson(update), "联系人记录失败");
        }
    }

    /**
     * @return List<PayoutPerson> 用户转账信息
     * @Title searchPerson
     * @Desc 获取当前登录用户转账信息
     * @Date 2024-04-28 12:20:19.777
     */
    public List<PayoutPersonDO> searchPerson() {
        LoginInfo info = SystemContext.getUser();
        Assert.isTrue(BeanUtils.isNotNull(info), "未找到登录信息");
        Long belongId = info.getBelongId();
        return personService.listByBelongId(belongId);
    }
}